﻿using University.Workshop.Business;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;

namespace University.Workshop.Tests
{
    [TestClass()]
    public class UserControllerTest
    {


        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        public void CheckIfExistsSameIdControllerTest()
        {
            string id = "1";
            bool expected = false;
            bool actual;
            actual = UserController.CheckIfExistsSameId(id);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void InsertUserTestNotEnterObligatoryFills()
        {
            string userId = string.Empty; 
            string userName = "Raul"; 
            string userSurname1 = ""; 
            string userSurname2 = string.Empty;
            string userTelephone = "4566689"; 
            string userCellPhone = string.Empty; 
            string userAddress = string.Empty; 
            string userPassword = string.Empty;
            string reUserPassword = string.Empty;
            string userOccupation = string.Empty;
            string expected = "Faltan llenar campos obligatorios, revise e ingrese, por favor!"; 
            string actual;
            actual = UserController.InsertUser(userId, userName, userSurname1, userSurname2, userTelephone, userCellPhone, userAddress, userPassword, reUserPassword, userOccupation);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void InsertUserTestNotMatchPasswords()
        {
            Random ran = new Random();
            int num = ran.Next(10000000);
            string userId = num.ToString(System.Globalization.CultureInfo.CurrentCulture);
            string userName = "Raul";
            string userSurname1 = "ibanez";
            string userSurname2 = "martinez";
            string userTelephone = "4566689";
            string userCellPhone = string.Empty; 
            string userAddress = string.Empty;
            string userPassword = "123";
            string reUserPassword = "678";
            string userOccupation = "Cajero";
            string expected = "Contraseñas no coinciden!"; 
            string actual;
            actual = UserController.InsertUser(userId, userName, userSurname1, userSurname2, userTelephone, userCellPhone, userAddress, userPassword, reUserPassword, userOccupation);
            Assert.AreEqual(expected, actual);
        }


        [TestMethod()]
        public void InsertUserTestNotFail()
        {
            Random ran = new Random();
            int num = ran.Next(10000000);
            string userId = num.ToString(System.Globalization.CultureInfo.CurrentCulture);
            string userName = "Raul";
            string userSurname1 = "Solis";
            string userSurname2 = "Gutierrez";
            string userTelephone = "4566689";
            string userCellPhone = string.Empty;
            string userAddress = string.Empty; 
            string userPassword = "123";
            string reUserPassword = "123";
            string userOccupation = "Cajero";
            string expected = "Usuario almacenado!";
            string actual;
            actual = UserController.InsertUser(userId, userName, userSurname1, userSurname2, userTelephone, userCellPhone, userAddress, userPassword, reUserPassword, userOccupation);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void CheckIfPasswordIsCorrectTest()
        {
            string password = "123";
            string id = "1";
            bool expected = true;
            bool actual;
            actual = UserController.CheckIfPasswordIsCorrect(password, id);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void UpdateUserPasswordTestFailPasswordIncorrect()
        {
            string id = "1"; 
            string password = string.Empty; 
            string rePassword = string.Empty; 
            string oldPassword = "giuf8y94huer";
            string expected = "La contraseña antigua es incorrecta.";
            string actual;
            actual = UserController.UpdateUserPassword(id, password, rePassword, oldPassword);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void UpdateUserPasswordTestFailNewPasswordDoNotMathOrNewPasswordIsEmpty()
        {
            string id = "1";
            string password = "mauri";
            string rePassword = "mauri2";
            string oldPassword = "123";
            string expected = "La contraseña nueva no coincide.";
            string actual;
            actual = UserController.UpdateUserPassword(id, password, rePassword, oldPassword);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void UpdateUserPasswordTestNotFail()
        {
            string id = "1";
            string password = "123";
            string rePassword = "123";
            string oldPassword = "123";
            string expected = "La modificacion de contraseña ha sido exitosa!";
            string actual;
            actual = UserController.UpdateUserPassword(id, password, rePassword, oldPassword);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void UpdateUserDataTestFailNotEnterObligaryFillsOfTheModifyForm()
        {
            string userId = "1";
            string userName = string.Empty;
            string userSurname1 = string.Empty;
            string userSurname2 = string.Empty;
            string userTelephone = "4567877";
            string userCellPhone = "70705040"; 
            string userAddress = "direccion"; 
            string userOccupation = "Administracion";
            string expected = "Faltan llenar campos obligatorios, revise e ingrese."; 
            string actual;
            actual = UserController.UpdateUserData(userId, userName, userSurname1, userSurname2, userTelephone, userCellPhone, userAddress, userOccupation);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void UpdateUserDataTestNotFail()
        {
            string userId = "1";
            string userName = "Javier";
            string userSurname1 = "Pardo";
            string userSurname2 = "Gomez";
            string userTelephone = "";
            string userCellPhone = "";
            string userAddress = "micasa";
            string userOccupation = "Administracion";
            string expected = "Usuario modificado!";
            string actual;
            actual = UserController.UpdateUserData(userId, userName, userSurname1, userSurname2, userTelephone, userCellPhone, userAddress, userOccupation);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for SearchAUser
        ///</summary>
        [TestMethod()]
        public void SearchAUserTest()
        {
            string id = "%" + "1" + "%";
            string name = "%" + "Javier" + "%";
            string surname1 = "%" + "Pardo" + "%";
            string surname2 = "%" + string.Empty + "%"; 
            DataTable actual;
            actual = UserController.SearchAUser(id, name, surname1, surname2);
            Assert.AreNotEqual(0,actual.Rows.Count);
        }
    }
}
